/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package org.myontology.users;

import java.lang.reflect.Field;

import model.Vocabulary;

import org.myontology.controller.AbstractAction;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.Mailer;
import org.myontology.controller.SesameSession;
import org.myontology.controller.pages.DisplayProperties;
import org.myontology.view.items.FormBuilderStates;
import org.openrdf.model.impl.LiteralImpl;

public class SaveUser extends AbstractAction {

	private SesameSession session;
	private CheckCaptcha checkCaptcha;
	private UserAlreadyExitsValidator exists;
	private Mailer mail;
	
	public void execute(AttributeHolder attributes) throws Exception {
		
		if (Login.LOGGED_IN.getValue(attributes)==null ||!Login.LOGGED_IN.getValue(attributes)) {
			DisplayProperties.HIDE_NAVIGATION.setValue(attributes, true);
			DisplayProperties.HIDE_TREE.setValue(attributes, true);
		}
		
		UserProtocol.TITLE.setValue(attributes, "Save User data");
		User user = UserProtocol.USER.getValue(attributes);
		user.parseAttributes(attributes);
		
		if (Login.CURRENT_USER.getValue(attributes)==null) {
			user.cleanValidators();
//			user.addValidator(checkCaptcha);
			user.addValidator(exists);
		}
	    
		
		if (user.isValid(attributes)) {
			session.update(user);
			
			if (Login.LOGGED_IN.getValue(attributes) == null || !Login.LOGGED_IN.getValue(attributes)) {
				String key = ""+Math.random()+System.currentTimeMillis();
				key = ""+key.hashCode();
				mail.mail(user, "Your myontology account confirmation key", 
						"You will be asked when you log in the first time:\n\n"
						+key,attributes);
				session.add(user.getURI(), Vocabulary.HASLOCK,new LiteralImpl(key) );
			}
			
			UserProtocol.EXISTING.setValue(attributes, new Boolean(true));
		}
		else {
			UserProtocol.EXISTING.setValue(attributes, new Boolean(false));
		}
	    UserProtocol.STATE.setValue(attributes, FormBuilderStates.EDIT);
		UserProtocol.USER.setValue(attributes, user);
		
	}

	private void fillFromRequestParams(AttributeHolder attributes,Object o)  throws Exception {
		Field[] fields = o.getClass().getDeclaredFields();
		for (int i=0;i<fields.length;i++) {
			String name = fields[i].getName();
			String value = attributes.getRequestParameter(name);
			fields[i].setAccessible(true);
			fields[i].set(o, value);
		}
	}
	
}
